﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Bombif.Game
{
    public partial class GameMain
    {

        enum titleScreenSelected //enum to check which menu option is selected
        {
            HostGame,
            JoinGame,
            Options,
            Exit,
            None
        }

        //position y of each menu option
        private float titleScreen_HostGamePosition = 200f;
        private float titleScreen_JoinGamePosition = 300f;
        private float titleScreen_OptionsPosition = 400f;
        private float titleScreen_ExitPosition = 500f;


        private titleScreenSelected titleScreen_SelectedMenu = titleScreenSelected.None;

        //mouse radius for selecting a menu option
        private float titleScree_MouseSelectionHeightRadius = 30f;
        private float titleScree_MouseSelectionWidthRadius = 50f;

        //Texture for background & others
        Texture2D BackGroundTitle;
        
        //Display the title screen.
        private void DrawTitleScreen()
        {
            GraphicsDevice.Clear(Color.Crimson);
        
            spriteBatch.Begin();

            spriteBatch.Draw(BackGroundTitle, Vector2.Zero, Color.White);


            Color colorText = Color.White;

            // Write Bombif at the top
            DrawCenter("Bomb'if", 50f, colorText);


            // Host Game
            if (titleScreen_SelectedMenu == titleScreenSelected.HostGame)
                colorText = Color.Blue;
            else
                colorText = Color.Green;

            DrawCenter("Host Game", titleScreen_HostGamePosition, colorText);

            // Join Game
            if (titleScreen_SelectedMenu == titleScreenSelected.JoinGame)
                colorText = Color.Blue;
            else
                colorText = Color.Green;

            DrawCenter("Join Game", titleScreen_JoinGamePosition, colorText);

            // Options
            if (titleScreen_SelectedMenu == titleScreenSelected.Options)
                colorText = Color.Blue;
            else
                colorText = Color.Green;

            DrawCenter("Options", titleScreen_OptionsPosition, colorText);

            // Exit
            if (titleScreen_SelectedMenu == titleScreenSelected.Exit)
                colorText = Color.Blue;
            else
                colorText = Color.Green;

            DrawCenter("Exit", titleScreen_ExitPosition, colorText);

            spriteBatch.End();
        }

        //write at the center of the screen
        private void DrawCenter(String message, float height, Color color)
        {
            Vector2 textSize = lucidaConsole.MeasureString(message);
            Vector2 textCenter = new Vector2(GraphicsDevice.Viewport.Width / 2, height);
            Vector2 textShadow = new Vector2(GraphicsDevice.Viewport.Width / 2 + 1, height + 1);
            spriteBatch.DrawString(lucidaConsole, message,
                textShadow - (textSize / 2), Color.Black);
            spriteBatch.DrawString(lucidaConsole, message,
                textCenter - (textSize / 2), color);
        }

        // Handles the inputs of the user on the title screen and handle the changes of state of the title screen
        protected void UpdateTitleScreen()
        {
            mouseState = Mouse.GetState();
            int xPosition = mouseState.X;
            int yPosition = mouseState.Y;
            if (TitleScreenCheckPosition(xPosition, yPosition, titleScreen_HostGamePosition))
            {
                titleScreen_SelectedMenu = titleScreenSelected.HostGame;
                if (mouseState.LeftButton == ButtonState.Pressed)
                {
                    //Activate the Lobby Menu (GameLobby)
                    StartLobby();
                    selectedScreen = SelectedScreen.HostGame;
                }
            }
            else if (TitleScreenCheckPosition(xPosition, yPosition, titleScreen_JoinGamePosition))
            {
                titleScreen_SelectedMenu = titleScreenSelected.JoinGame;
                if (mouseState.LeftButton == ButtonState.Pressed)
                {
                    //Activate the Lobby Menu (GameLobby)
                }
            }
            else if (TitleScreenCheckPosition(xPosition, yPosition, titleScreen_OptionsPosition))
            {
                titleScreen_SelectedMenu = titleScreenSelected.Options;
                if (mouseState.LeftButton == ButtonState.Pressed)
                {
                    //Activate the Option Menu (GameOptions)
                }
            }
            else if (TitleScreenCheckPosition(xPosition, yPosition, titleScreen_ExitPosition))
            {
                titleScreen_SelectedMenu = titleScreenSelected.Exit;
                if (mouseState.LeftButton == ButtonState.Pressed)
                {
                    Exit();
                }

            }
            else
            {
                titleScreen_SelectedMenu = titleScreenSelected.None;
            }
        }

        //Check if a menu option is selected
        private bool TitleScreenCheckPosition(int xPosition, int yPosition, float menuPosition)
        {
            return xPosition < (GraphicsDevice.Viewport.Width / 2 + titleScree_MouseSelectionWidthRadius)
                            && xPosition > (GraphicsDevice.Viewport.Width / 2 - titleScree_MouseSelectionWidthRadius)
                            && yPosition < (menuPosition + titleScree_MouseSelectionHeightRadius)
                            && yPosition > (menuPosition - titleScree_MouseSelectionHeightRadius);
        }
    }
}
